import axios from 'axios'

export function maskToLastLimit(val, visibleLength = 5, maskChar = '*', maxMaskLength = 5) {
  if (!val) return ''
  const str = String(val)
  const len = str.length

  if (len <= visibleLength) return str

  const maskLen = Math.min(len - visibleLength, maxMaskLength)
  return `${maskChar.repeat(maskLen)} ${str.slice(-visibleLength)}`
}
export function createRangeSorter(propName) {
  return (a, b) => {
    const toNumber = (val) => {
      if (!val) return 0
      return parseFloat(val.toString().replace('%', '')) || 0
    }
    return toNumber(a[propName]) - toNumber(b[propName])
  }
}
export function createDateSorter(propName) {
  return (a, b) => {
    const toTime = (val) => {
      if (!val) return 0
      const ts = Date.parse(val.replace(' ', 'T'))
      return isNaN(ts) ? 0 : ts
    }
    return toTime(a[propName]) - toTime(b[propName])
  }
}
